package cc.fs.zhihuapp.rest.asynchttp;


import android.util.Log;

import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;


import java.util.HashMap;
import java.util.Objects;

import cc.fs.zhihuapp.model.Category;
import cc.fs.zhihuapp.model.DayNews;
import cc.fs.zhihuapp.model.NewsHtml;
import cc.fs.zhihuapp.utils.MyCallback;

/**
 * Created by fostion on 2015/9/9.
 * 使用gson解析可以快速解析成object
 */
public class Api {

    public static Gson gson = new Gson();
    private static String HOST = "http://news-at.zhihu.com";
    private static String API = "/api";
    private static String VERSION = "/3";
    private static String GET_CATEGORY = HOST+API+VERSION+"/themes";
    private static String GET_NEWS = HOST+API+VERSION+"/news";

    public static void getLastNews(final MyCallback.NetworkCallback0<DayNews> callback){
        ApiHelper.get(GET_NEWS+"/latest", new ApiHandler() {
            @Override
            public void onSuccess(JSONObject object) {
                DayNews dayNews = gson.fromJson(object.toString(),DayNews.class);
                callback.success(dayNews);
            }

            @Override
            public void onFailure(String error) {
                callback.failure(error);
            }
        });
    }

    public static void getCategory(final MyCallback.NetworkCallback0<Category> callback){
        ApiHelper.get(GET_CATEGORY, new ApiHandler() {
            @Override
            public void onSuccess(JSONObject object) {
                Category category = gson.fromJson(object.toString(),Category.class);
                callback.success(category);
            }

            @Override
            public void onFailure(String error) {
                callback.failure(error);
            }
        });
    }

    public static void getOldNews(String updateTime,final MyCallback.NetworkCallback0<DayNews> callback){
        ApiHelper.get(GET_NEWS+"/before/"+updateTime, new ApiHandler() {
            @Override
            public void onSuccess(JSONObject object) {
                DayNews dayNews = gson.fromJson(object.toString(),DayNews.class);
                callback.success(dayNews);
            }

            @Override
            public void onFailure(String error) {
                callback.failure(error);
            }
        });
    }

    public static void getNewsDetail(String newsId,final MyCallback.NetworkCallback0<NewsHtml> callback){
        ApiHelper.get(GET_NEWS + "/" + newsId, new ApiHandler() {
            @Override
            public void onSuccess(JSONObject object) {
                NewsHtml newsHtml = gson.fromJson(object.toString(), NewsHtml.class);
                callback.success(newsHtml);
            }

            @Override
            public void onFailure(String error) {
                callback.failure(error);
            }
        });
    }

}
